package com.qc.ayouth.shop.admin.controller.common;

import com.qc.ayouth.shop.admin.beans.AdminResponseVO;
import com.qc.ayouth.shop.core.beans.bo.ServiceResultBO;
import com.qc.ayouth.shop.core.beans.dto.StorageDto;
import com.qc.ayouth.shop.core.constants.ServiceConstants;
import com.qc.ayouth.shop.core.service.storage.StorageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * Created by Haojg on 2020/7/11
 */
@RestController
@RequestMapping("/api/admin/storage")
@Validated
@Slf4j
public class AdminStorageController {

    @Autowired
    StorageService storageService;

    @RequiresPermissions("storage")
    @PostMapping("/create")
    public AdminResponseVO create(@RequestParam("file") MultipartFile file) throws IOException {
        log.info("admin storage create start");
        AdminResponseVO vo = new AdminResponseVO();
        String originalFilename = file.getOriginalFilename();
        StorageDto storageDto = storageService.store(file.getInputStream(), file.getSize(), file.getContentType(), originalFilename);
        // storageDto 返回来的数据，保存到数据库中
        vo.add("storageInfo", storageDto);
        vo.ok();
        log.info("admin storage create end");
        return vo;
    }

}
